package com.tanhua.server.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.dubbo.api.mongo.UserLikeApi;
import com.tanhua.server.interceptors.UserHolder;
import com.tanhua.vo.TanhuaUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.yetus.audience.InterfaceAudience;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TanhuaService {

    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private UserLikeApi userLikeApi;
    @Autowired
    private UserService userService;
    @Reference
    private FriendApi friendApi;
    //
    /**
     * 探花用户列表展示
     * @return
     */
    public ResponseEntity cards() {
        //查询自己的Id
        Long userId = UserHolder.getUserId();

        List<UserInfo> userInfoList = userInfoApi.findAll();
        TanhuaUser tanhuaUser = null;
        List<TanhuaUser> tanhuaUsers = new ArrayList<>();
        if (userInfoList != null) {

            for (UserInfo userInfo : userInfoList) {
                //先排除自己
                if(userInfo.getId().longValue()==userId.longValue()){
                    continue;
                }
                tanhuaUser = new TanhuaUser();
                //就是从UserInfo中 但是UserInfo中的tags是字符串这里要的是数组
                if(userInfo.getTags()!=null){
                    tanhuaUser.setTags(userInfo.getTags().split(","));
                }
                Integer id = Integer.valueOf(userInfo.getId().toString());
                tanhuaUser.setId(id);
                //Integer age = userInfo.getAge();
                //tanhuaUser.setAge(age);
                BeanUtils.copyProperties(userInfo,tanhuaUser);
                tanhuaUsers.add(tanhuaUser);
            }
        }
        return ResponseEntity.ok(tanhuaUsers);
    }


    /**
     *  右划喜欢
     * @param id
     * @return null
     */
    public ResponseEntity love(Integer id) {
        //判断当前登录用户和展示用户的关系
        Long userId = UserHolder.getUserId();
        //如果是互相喜欢和喜欢不进行操作
        Boolean exists1 = userLikeApi.queryMyFans(userId, id.longValue());
        Boolean exists2 = friendApi.queryAllLike(userId, id.longValue());
        //如果是粉丝
        Boolean exists  = userLikeApi.queryMyFans( id.longValue(),UserHolder.getUserId());
        if (exists) {
            //判断表中是否有假数据
            if (!exists2) {
                //1、成为好友
                friendApi.save(userId, id.longValue());
            }
            //2、注册到环信上
            //huanXinTemplate.contactUsers(userId,fansUserId); //暂时在开发时先忽略
            // 3、删除粉丝关系  举例 1号用户要删除的粉丝是58号用户  userId =1   fansUserId=58
            userLikeApi.delete(id.longValue(), userId);
        } else if (!exists && !exists1 && !exists2) {
            //如果是陌生人，成为他的粉丝
            userLikeApi.save(userId,id.longValue());
        }
        return ResponseEntity.ok(null);
    }


    /**
     *  左滑 不喜欢
     * @param id
     * @return null
     */
    public ResponseEntity unlove(Integer id) {
        //判断当前登录用户和展示用户的关系
        Long userId = UserHolder.getUserId();
        //如果是粉丝/或陌生人      无操作

        // 如果是喜欢     删除喜欢
        Boolean exists  = userLikeApi.queryMyFans( UserHolder.getUserId(),id.longValue());
        if (exists) {
            userLikeApi.delete(userId,id.longValue());
        }

        //如果是互相喜欢  删除互相喜欢 粉丝增加
        Boolean existsFriend = friendApi.queryAllLike(userId, id.longValue());
        if (existsFriend) {
            friendApi.delete(userId,id.longValue());
            //判断表中是否有假数据
            if (!userLikeApi.queryMyFans(id.longValue(), UserHolder.getUserId())) {
                userLikeApi.save(id.longValue(), userId);
            }
        }
        return ResponseEntity.ok(null);
    }
}
